/*
 *所有关于Student类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*StudentService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.MyTable.IRepositories;
using System.Collections.Generic;
using VOL.Core.ManageUser;

namespace VOL.MyTable.Services
{
    public partial class StudentService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IStudentRepository _repository;//访问数据库
        WebResponseContent webResponse = new WebResponseContent();




        [ActivatorUtilitiesConstructor]
        public StudentService(
            IStudentRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {

            AddOnExecute = (SaveModel saveModel) =>
            {
                //如果返回false,后面代码不会再执行


                //var studid=saveDataModel.MainData.SingleOrDefault(x=>x.Key==""
                //if (Stud_gradeList.Exists(x => x.stud_id == 1))
                //return webResponse.Error("明细数量必须大于20");

                //saveDataModel.MainData.Add("ADD_ALL", 120);
                return webResponse.OK();
            };
            AddOnExecuting = (Student student, object list) =>
            {

                //student.add_all = 120;
                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }

        public override PageGridData<Student> GetPageData(PageDataOptions options)
        {
            //添加查询和额外数据给前端
            var sgdb = repository.DbContext.Set<Stud_grade>();
            var part_sg=sgdb.GroupBy(m=>new { m.stud_id}).Select(t => new { t.Key.stud_id, sum_all=t.Sum(c=>c.grade) }).ToList();
            //var result = base.GetPageData(options);

            //result.extra = part_sg;//result.rows.Select(t => new { t.stud_id, t.name }).ToList();

            QuerySql = $@"select * from student where CreateID={UserContext.Current.UserId}";//@是避免转移字符、$是动态参数替换

            var rtn = base.GetPageData(options);
            rtn.extra = part_sg;//添加额外数据给web客户端
            return rtn;


            /*PageGridData <Student> gridData = base.GetPageData(options);
            gridData.extra = gridData.rows.Select(t =>new { t.stud_id,t.name}).ToList();
            
            return gridData;*/
        }

        /// <summary>
        /// 此方法，如果有合计字段必须要有
        /// </summary>
        /// <typeparam name="detail"></typeparam>
        /// <param name="queryeable"></param>
        /// <returns></returns>
        protected override object GetDetailSummary<detail>(IQueryable<detail> queryeable)
        {
            return (queryeable as IQueryable<Stud_grade>).GroupBy(x => 1).Select(x => new
            {
                //Weight/Qty注意大小写和数据库字段大小写一样
                grade = x.Sum(o => o.grade)    
            }).FirstOrDefault();
        }


    }
}
